package com.jnshu.web;

import com.jnshu.service.LearningService;
import com.jnshu.service.PersonService;
import com.jnshu.tools.resultUtils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class LearningController {

    @Autowired(required = false)
    LearningService learningService;

    //文章列表
    @GetMapping("/u/cotent/list")
    ResultBean uSelectContentList(@RequestParam(value = "grade", required = false) Integer grade,
                                  @RequestParam(value = "subject", required = false) Integer subject,
                                  @RequestParam(value = "classify", required = false) Integer classify,
                                  @RequestParam(value = "type", required = false) Integer type,
                                  @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                  @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                  HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));
        return learningService.uSelectContentList(grade, subject, classify, type, pageSize, pageNum, userId);
    }


    //文章搜索
    @GetMapping("/u/ucotent")
    ResultBean uSelectContent(@RequestParam(value = "title", required = false) String title,
                              @RequestParam(value = "classify", required = false) Integer classify,
                              @RequestParam(value = "pageSize", required = false) Integer pageSize,
                              @RequestParam(value = "pageNum", required = false) Integer pageNum,
                              HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));
        return learningService.uSelectContent(title, classify, pageSize, pageNum, userId);
    }


    //文章详情
    @GetMapping("/u/cotent/details")
    ResultBean selectByPrimaryKey(@RequestParam(value = "id", required = false) Long id, HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));
        return learningService.selectByPrimaryKey(id, userId);
    }

    //视频详情
    @GetMapping("/u/video/details")
    ResultBean selectByPrimaryKeyVideo(@RequestParam(value = "id", required = false) Long id,
                                       HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));

        return learningService.selectByPrimaryKeyVideo(id, userId);
    }

    //收藏
    @PostMapping("/u/search/collection")
    ResultBean insert(@RequestParam(value = "classify", required = false) Integer classify,
                      @RequestParam(value = "courseId", required = false) Long courseId,
                      HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));

        return learningService.insert(classify, courseId, userId);
    }

    //收藏状态
    @GetMapping("/u/search/collection")
    ResultBean selectStatus(@RequestParam(value = "courseId", required = false) Long courseId,
                            HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("controller层selectStatus方法的courseId为" + userId + "======userId为" + userId);
        return learningService.selectStatus(courseId, userId);
    }

    //取消收藏
    @DeleteMapping("/u/search/collection")
    ResultBean deleteStatus(@RequestParam(value = "courseId", required = false) Long courseId,
                            HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));

        return learningService.deleteStatus(courseId, userId);
    }

    //获取记录
    @GetMapping("/u/search/history")
    ResultBean getHistory(@RequestParam(value = "classify", required = false) Integer classify,
                          HttpServletRequest request) {
        String userId = (String) request.getAttribute("id");
        return learningService.getHistory(classify, userId);
    }

    //删除记录
    @DeleteMapping("/u/search/history")
    public ResultBean deleteHistory(@RequestParam(value = "classify", required = false) Integer classify,
                                    HttpServletRequest request) {
        String userId = (String) request.getAttribute("id");
        return learningService.deleteHistory(classify, userId);
    }

    //    任务状态
    @GetMapping("/u/task/details")
    public ResultBean selectByStatus(Long taskId, HttpServletRequest request) {
        long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("controller层celectByStaus方法的taskId值为" + taskId);
        return learningService.selectByStatus(taskId, userId);

    }
}
